package main.util
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import gs.TweenLite;
	import gs.easing.Back;
	import gs.easing.Elastic;
	import gs.easing.Quart;
	
	import main.Conf;
	
	public class ToolTip extends Sprite
	{
		
		private var yoffset:Number = 20;
		private var tf:TextField;
		private var bg:Shape = new Shape();
		private var container:Sprite;
		
		private var textFormat:TextFormat = new TextFormat(Conf.FONT_DEFAULT,15);
		
		private var shadow:DropShadowFilter = new DropShadowFilter(3,-90,0,.5,12,12);
		
		public function ToolTip()
		{
			filters = [shadow];
			
			addChild(bg);
			tf = createTextfield();
			addChild(tf);
			mouseEnabled = false;
			mouseChildren = false;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			alpha = 0;
		}
		
		private function onEnterFrame(e:Event):void {
			if (stage) {
				x += (stage.mouseX-x)*.2;
				y += (stage.mouseY-y)*.2;
			}
		}
		
		
		public function hide():void {
			TweenLite.to(this, .1, {
				scaleX: .1,
				ease: Quart.easeOut
			});
			TweenLite.to(this, .2, {
				scaleY: .1,
				alpha: 0,
				ease: Back.easeIn
			});
			TweenLite.to(this, .4, {
				rotation: Util.rand(100),
				ease: Elastic.easeOut
			});		
		}
		
		public function setText(t:String):void {
			tf.text = " "+t+" ";
			tf.setTextFormat(textFormat);
			drawBackground();
			
			bg.x = -bg.width/2;
			bg.y = -(bg.height+yoffset);
			tf.x = bg.x;
			tf.y = bg.y;
			
			TweenLite.to(this, .6, {
				scaleY: 1,
				alpha: 1,
				ease: Elastic.easeOut
			});
			TweenLite.to(this, .8, {
				scaleX: 1,
				ease: Elastic.easeOut
			});
			TweenLite.to(this, 2, {
				rotation: Util.rand(30),
				ease: Elastic.easeOut
			});			
		}
		
		private function createTextfield():TextField {
			var t:TextField = new TextField();
			t.autoSize = TextFieldAutoSize.LEFT;
			t.text = "ToolTip";
			t.selectable = false;
			t.setTextFormat(textFormat);
			t.embedFonts = true;
			return t;
		}
		
		private function drawBackground():void {
			bg.graphics.clear();
			bg.graphics.beginFill(0xe4deba);
			bg.graphics.drawRect(0, 0, tf.width, tf.height);
			bg.graphics.endFill();
		}

	}
}